package SwordOffer2;

public class day9_1 {
    /*
     * 1.创建reverseList()函数反转链表，
     * 2.创建fast，slow指针计算链表中点，newHead记录反转的后半部链表
     * 3.循环比较head，newHead中的值是否一样，不一致返回false，一致返回true
     * */
    public boolean isPalindrome(ListNode head) {
//        ListNode listNode =new ListNode();
        boolean res = true;
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        ListNode newHead = reverseList(slow);
        /*         listNode.printValue(newHead);*/
        slow.next = null;
        while (newHead != null) {
            if (newHead.val != head.val) {
                res = false;
                break;
            }
            head = head.next;
            newHead = newHead.next;
        }
        return res;
    }

    public ListNode reverseList(ListNode head) {
        ListNode cur = head;
        ListNode pre = null;
        while (cur != null) {
            ListNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }
}
